//
//  CFBaseViewController.m
//  WealthElves
//
//  Created by Wealthwiz on 2017/4/25.
//  Copyright © 2017年 Wealthwiz. All rights reserved.
//

#import "CFBaseViewController.h"

#define HBBVC_NAV_BUTTON_MARGIN     12
#define HBBVC_NAV_BUTTON_COUNT      2
#define HBBVC_NAV_SIDE_SPACE        -8
#define HBBVC_NAV_DOT_SIZE          8

#define HBBVC_NAV_DOT_VIEW_ID       1000

@interface CFBaseViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) NSMutableArray *leftNavButtons;
@property (nonatomic, strong) NSMutableArray *rightNavButtons;

@end

@implementation CFBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *statusView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, 21)];
    statusView.backgroundColor = [UIColor colorWithHexString:@"#168ec8"];
    [self.view addSubview:statusView];
    
    
    self.view.backgroundColor = [UIColor colorWithRed:232/255.0 green:236/255.0 blue:239/255.0 alpha:1.0];
    
    
    
    // Do any additional setup after loading the view.
    
  //  self.view.backgroundColor = [UIColor colorWithHexString:@"0xf0f0f0"];//f0f0f0
    //self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:18]};
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
    
    //NAVBar的背景颜色，backgroundColor和tintColor都不行
   // self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#4285f4"];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"CF_Finanical_Nav_Bg"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    //self.navigationController.navigationBar.translucent = NO;
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    NSString *buttonLeftTitle = [self titleForLeftNavigationButton];
    if(buttonLeftTitle != nil && buttonLeftTitle.length > 0) {
        UIButton *leftNavButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftNavButton.frame = CGRectMake(0,0,30,30);
        [leftNavButton addTarget:self
                          action:@selector(onLeftNavigationButton:)
                forControlEvents:UIControlEventTouchUpInside];
        [leftNavButton setBackgroundImage:[UIImage imageNamed:buttonLeftTitle] forState:UIControlStateNormal];
        UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftNavButton];
        self.navigationItem.leftBarButtonItem = leftBarButton;
    }
    
    NSString *buttonRightTitle = [self titleForRightNavigationButton];
    if(buttonRightTitle != nil && buttonRightTitle.length > 0) {
        UIButton *rightNavButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightNavButton.frame = CGRectMake(0,0,24,24);
        [rightNavButton addTarget:self
                           action:@selector(onRightNavigationButton:)
                 forControlEvents:UIControlEventTouchUpInside];
        NSString *buttonRightTitleType = [self titleForRightNavigationButtonWithType];
        if ([buttonRightTitleType isEqualToString:@"1"]) {
            // 1代表图片 2代表文字
            [rightNavButton setBackgroundImage:[UIImage imageNamed:buttonRightTitle] forState:UIControlStateNormal];
        }
        else if ([buttonRightTitleType isEqualToString:@"2"])
        {
            UIFont *iconFont = [UIFont fontWithName:@"PingFangSC-Regular" size: kNavItemFontSize];
            
            CGSize desiredSize = [buttonRightTitle sizeWithAttributes:@{NSFontAttributeName:iconFont}];
            CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
            rightNavButton.frame = CGRectMake(0,
                                              0,
                                              desiredSize.width + HBBVC_NAV_BUTTON_MARGIN,
                                              navBarHeight);
            
            
            rightNavButton.titleLabel.font = iconFont;
            [rightNavButton setTitleColor:kNavItemFontColor forState:UIControlStateNormal];
            [rightNavButton setTitleShadowColor:kNavItemFontColor forState:UIControlStateNormal];
            [rightNavButton setTitle:buttonRightTitle forState:UIControlStateNormal];
        }
        UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightNavButton];
        self.navigationItem.rightBarButtonItem = rightBarButton;
    }
    
    return;
    
    UIFont *iconFont = [UIFont fontWithName:@"iconfont" size: kNavItemFontSize];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    CGFloat scale = (kUIScreenWidth / 320.0);
    negativeSpacer.width = HBBVC_NAV_SIDE_SPACE * scale;
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
    
    _leftNavButtons = [NSMutableArray new];
    _rightNavButtons = [NSMutableArray new];
    
    /// Buttons on left side
    NSMutableArray *leftNavItems = [NSMutableArray new];
    for(int i=0; i<HBBVC_NAV_BUTTON_COUNT; ++i) {
        NSString *buttonTitle = [self titleForLeftNavigationButtonAtIndex:i];
        if(buttonTitle != nil && buttonTitle.length > 0) {
            UIButton *navButton = [UIButton buttonWithType:UIButtonTypeSystem];
            CGSize desiredSize = [buttonTitle sizeWithAttributes:@{NSFontAttributeName:iconFont}];
            navButton.frame = CGRectMake(0,
                                         0,
                                         desiredSize.width + HBBVC_NAV_BUTTON_MARGIN,
                                         navBarHeight);
            [navButton addTarget:self
                          action:@selector(onLeftSideNavigationButtonTouched:)
                forControlEvents:UIControlEventTouchUpInside];
            navButton.titleLabel.font = iconFont;
            [navButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [navButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [navButton setTitle:buttonTitle forState:UIControlStateNormal];
            navButton.tag = i;
            [self.leftNavButtons addObject:navButton];
            // create dot badge
            UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(navButton.frame.size.width-HBBVC_NAV_DOT_SIZE,
                                                                       HBBVC_NAV_DOT_SIZE,
                                                                       HBBVC_NAV_DOT_SIZE, HBBVC_NAV_DOT_SIZE)];
            dotView.backgroundColor = [UIColor redColor];
            dotView.layer.cornerRadius = HBBVC_NAV_DOT_SIZE/2;
            dotView.hidden = YES;
            dotView.tag = HBBVC_NAV_DOT_VIEW_ID;
            [navButton addSubview:dotView];
            // create nav item
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:navButton];
            [leftNavItems addObject:leftItem];
        }
    }
    if(leftNavItems.count > 0) {
        [leftNavItems insertObject:negativeSpacer atIndex:0];
        self.navigationItem.leftBarButtonItems = leftNavItems;
    }
    
    /// Buttons on right side
    NSMutableArray *rightNavItems = [NSMutableArray new];
    for(int i=0; i<HBBVC_NAV_BUTTON_COUNT; ++i) {
        NSString *buttonTitle = [self titleForRightNavigationButtonAtIndex:i];
        if(buttonTitle != nil && buttonTitle.length > 0) {
            UIButton *navButton = [UIButton buttonWithType:UIButtonTypeSystem];
            CGSize desiredSize = [buttonTitle sizeWithAttributes:@{NSFontAttributeName:iconFont}];
            navButton.frame = CGRectMake(0,
                                         5,
                                         desiredSize.width + HBBVC_NAV_BUTTON_MARGIN,
                                         navBarHeight);
            [navButton addTarget:self
                          action:@selector(onRightSideNavigationButtonTouched:)
                forControlEvents:UIControlEventTouchUpInside];
            navButton.titleLabel.font = iconFont;
            navButton.titleLabel.textAlignment = NSTextAlignmentRight;
            [navButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [navButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [navButton setTitle:buttonTitle forState:UIControlStateNormal];
            navButton.tag = i;
            [self.rightNavButtons addObject:navButton];
            // create dot badge
            UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(navButton.frame.size.width-HBBVC_NAV_DOT_SIZE,
                                                                       HBBVC_NAV_DOT_SIZE,
                                                                       HBBVC_NAV_DOT_SIZE, HBBVC_NAV_DOT_SIZE)];
            dotView.backgroundColor = [UIColor redColor];
            dotView.layer.cornerRadius = HBBVC_NAV_DOT_SIZE/2;
            dotView.hidden = YES;
            dotView.tag = HBBVC_NAV_DOT_VIEW_ID;
            [navButton addSubview:dotView];
            // create nav item
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:navButton];
            [rightNavItems addObject:leftItem];
        }
    }
    if(rightNavItems.count > 0) {
        [rightNavItems insertObject:negativeSpacer atIndex:0];
        self.navigationItem.rightBarButtonItems = rightNavItems;
    }
    
    // add title view if needed
    UIView *titleView = [self titleViewForNavigationBar];
    if(titleView != nil)
        self.navigationItem.titleView = titleView;
    
    if([self supportPanGestureToPop])
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    else {
        // hook pan gesture if not support gesture popping
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                     action:@selector(panGestureRecognizer:)];
        [self.view addGestureRecognizer:panGesture];
    }
}
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)pan {
}
- (BOOL)supportPanGestureToPop {
    return YES;
}
- (NSString *)titleForLeftNavigationButtonAtIndex:(NSInteger)index {
    NSString *title = @"";
    switch (index) {
        case 0:
            title = [self titleForLeftNavigationButton];
            break;
        case 1:
            title = [self titleForSecondaryLeftNavigationButton];
            break;
        default:
            break;
    }
    return title;
}

- (NSString *)titleForRightNavigationButtonAtIndex:(NSInteger)index {
    NSString *title = @"";
    switch (index) {
        case 0:
            title = [self titleForRightNavigationButton];
            break;
        case 1:
            title = [self titleForSecondaryRightNavigationButton];
            break;
        default:
            break;
    }
    return title;
}

- (UIView *)titleViewForNavigationBar {
    return nil;
}

- (NSString *)titleForLeftNavigationButton {
    return nil;
}

- (NSString *)titleForSecondaryLeftNavigationButton {
    return nil;
}

- (NSString *)titleForRightNavigationButton {
    return nil;
}
- (NSString *)titleForRightNavigationButtonWithType {
    return nil;
}
- (NSString *)titleForSecondaryRightNavigationButton {
    return nil;
}

- (void)titleColorForLeftNavButton:(UIColor *)color
{
    UIButton *leftButton = self.leftNavButtons[0];
    [leftButton setTitleColor:color forState:UIControlStateNormal];
}

- (void)onLeftSideNavigationButtonTouched:(UIButton *)sender {
    // clear badge on touched
    UIView *dotView = (UIView *)[sender viewWithTag:HBBVC_NAV_DOT_VIEW_ID];
    if(dotView)
        dotView.hidden = YES;
    // fire event
    NSInteger index = sender.tag;
    switch (index) {
        case 0:
            [self onLeftNavigationButton:sender];
            break;
        case 1:
            [self onSecondaryLeftNavigationButton:sender];
            break;
        default:
            break;
    }
}

- (void)onRightSideNavigationButtonTouched:(UIButton *)sender {
    // clear badge on touched
    UIView *dotView = (UIView *)[sender viewWithTag:HBBVC_NAV_DOT_VIEW_ID];
    if(dotView)
        dotView.hidden = YES;
    // fire event
    NSInteger index = sender.tag;
    switch (index) {
        case 0:
            [self onRightNavigationButton:sender];
            break;
        case 1:
            [self onSecondaryRightNavigationButton:sender];
            break;
        default:
            break;
    }
}

- (void)onLeftNavigationButton:(UIButton *)sender {
}

- (void)onSecondaryLeftNavigationButton:(UIButton *)sender {
}

- (void)onRightNavigationButton:(UIButton *)sender {
}

- (void)onSecondaryRightNavigationButton:(UIButton *)sender {
}

- (void)createNavigation:(NSString *)naviTitle{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, Width, 44)];
    headView.backgroundColor = [UIColor colorWithHexString:@"#168ec8"];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(35, 0, Width - 35, 44)];
    label.text = naviTitle;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    [headView addSubview:label];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 35, 44);
    [backButton setImage:[UIImage imageNamed:@"homeReturen"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(12, 15, 12, 10);
    [headView addSubview:backButton];
    
    [self.view addSubview:headView];
}

- (void)backClick:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
